﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace JobColler.builder
{
    class SearchConfigBuilder
    {
        /// <summary>
        /// 关键词
        /// </summary>
        string keyword;
        /// <summary>
        /// 城市
        /// </summary>
        string address;
        /// <summary>
        /// 发布时间
        /// </summary>
        string time;
        /// <summary>
        /// 薪资范围
        /// </summary>
        string remuner;
        /// <summary>
        /// 企业性质
        /// </summary>
        string cotype;
        /// <summary>
        /// 工作经验
        /// </summary>
        string workyear;

        /// <summary>
        /// 学历要求
        /// </summary>
        string degreefrom;
        /// <summary>
        /// 公司规模
        /// </summary>
        string companysize;

        public string Keyword { get => keyword; set => keyword = value; }
        public string Address { get => address; set => address = value; }
        public string Time { get => time; set => time = value; }
        public string Remuner { get => remuner; set => remuner = value; }
        public string Cotype { get => cotype; set => cotype = value; }
        public string Workyear { get => workyear; set => workyear = value; }
        public string Degreefrom { get => degreefrom; set => degreefrom = value; }
        public string Companysize { get => companysize; set => companysize = value; }

        /// <summary>
        /// 城市对照表
        /// </summary>
        public static Dictionary<String, String> cityMapper = new Dictionary<string, string>();
        /// <summary>
        /// 工作经验对照表
        /// </summary>
        private static Dictionary<String, String> workMapper = new Dictionary<string, string>();
        /// <summary>
        /// 企业性质对照表
        /// </summary>
        private static Dictionary<String, String> coTypeMapper = new Dictionary<string, string>();
        /// <summary>
        /// 公司规模对照表
        /// </summary>
        private static Dictionary<String, String> coSizeMapper = new Dictionary<string, string>();
        /// <summary>
        /// 发布时间对照表
        /// </summary>
        private static Dictionary<String, String> timeMapper = new Dictionary<string, string>();
        /// <summary>
        /// 薪资范围对照表
        /// </summary>
        private static Dictionary<String, String> remuneMapper = new Dictionary<string, string>();
        /// <summary>
        /// 学历对照表
        /// </summary>
        private static Dictionary<String, String> degreeMapper = new Dictionary<string, string>();


        public String getSearchUrl(int page)
        {
            StringBuilder url = new StringBuilder("https://search.51job.com/list/");
            //拼接城市
            url.Append(cityMapper[address]);
            url.Append(",000000,0000,00,");
            //拼接发布时间
            url.Append(timeMapper[time]);
            url.Append(",");
            //拼接薪资范围
            url.Append(remuneMapper[remuner]);
            url.Append(",");
            //拼接关键词
            url.Append(System.Web.HttpUtility.UrlEncode(keyword));
            url.Append(",2,");
            //拼接页码
            url.Append(page);
            url.Append(".html?lang=c&stype=1&postchannel=0000&workyear=");
            //拼接工作经验
            url.Append(workMapper[workyear]);
            url.Append("&cotype=");
            //拼接企业性质
            url.Append(coTypeMapper[cotype]);
            url.Append("&degreefrom=");
            //拼接学历
            url.Append(degreeMapper[degreefrom]);
            url.Append("&jobterm=99&companysize=");
            //拼接企业规模
            url.Append(coSizeMapper[companysize]);
            url.Append("&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare=");
            return url.ToString();

        }

        static SearchConfigBuilder()
        {
            Type currentType = typeof(SearchConfigBuilder);
            MemberInfo[] fields = currentType.GetMembers(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);    //获取所有成员
            foreach (MemberInfo field in fields)
            {
                if (!field.Name.EndsWith("Mapper"))
                {
                    continue;
                }
                String context = JobColler.Properties.Resources.ResourceManager.GetString(field.Name);
                if (String.IsNullOrEmpty(context))
                {
                    continue;
                }
                String[] datas = context.Split(Environment.NewLine.ToCharArray());
                Dictionary<String, String> mapper = (Dictionary<String, String>)((FieldInfo)field).GetValue(new SearchConfigBuilder());
                foreach (String line in datas)
                {
                    if (String.IsNullOrEmpty(line))
                    {
                        continue;
                    }
                    String[] keyAndValue = line.Split(":".ToCharArray());

                    if (mapper.ContainsKey(keyAndValue[0]))
                    {
                        continue;
                    }
                    mapper.Add(keyAndValue[0], keyAndValue[1]);
                }

            }

        }
    }
}
